package com.hyh.prize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyh.prize.entity.Dict;
import com.hyh.prize.entity.TabSaleplanGiftcode;
import com.hyh.prize.entity.TabSaleplanGiftstock;
import com.hyh.prize.mapper.DictMapper;
import com.hyh.prize.service.DictSerive;
import com.hyh.prize.service.GgkStockService;
import com.hyh.prize.service.GiftService;
import com.hyh.prize.vo.LevelAndNumver;
import com.hyh.prize.vo.Yyt;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictSerive {

    @Resource
    private GgkStockService ggkStockService;
    @Resource
    private GiftService giftService;

    @Override
    public List<Yyt> getYyt(long id) {

        List<Yyt> yytlist=new ArrayList<>();

        //获得地区编码
        QueryWrapper<Dict> queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent",26);
        queryWrapper.eq("value",id);
        Dict dict1 = this.getOne(queryWrapper);
        //通过地区id查询改地区下的营业厅code
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("parent",dict1.getCode());

        List<Dict> list = this.list(queryWrapper1);
        //通过营业厅查询所有的营业厅名称，地区名称
        for (Dict dict : list) {
            Yyt yyt=new Yyt();
            yyt.setYid(dict.getCode());
            yyt.setYName(dict.getCodeName());
            //通过地区code获取地区名称
            yyt.setDName(this.getDName(dict.getParent()));
            yytlist.add(yyt);
        }
        return yytlist;
    }



    //根据code获取地区名称
    public String getDName(String code){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent","26");
        queryWrapper.eq("code",code);
        Dict dict = this.getOne(queryWrapper);
        if(dict!=null){
            return dict.getCodeName();
        }
        return null;

    }
    //根据营业厅id获取营业厅刮刮卡配置信息
    @Override
    public Yyt getYyt(Dict dict) {
        Yyt yyt=new Yyt();

        yyt.setYid(dict.getCode());
        yyt.setYName(dict.getCodeName());
        //通过地区code获取地区名称
        yyt.setDName(this.getDName(dict.getParent()));
        return yyt;
    }
    //通过营业厅id获取礼品配置信息
    public List<LevelAndNumver> getLeveInfo(String code){
        //通过营业厅id获取礼品库存信息
        List<TabSaleplanGiftstock> stockList = ggkStockService.getStockList(code);

        List<LevelAndNumver> lList=new ArrayList<>();


        //通过循环查询所有奖品配置信息
        for (TabSaleplanGiftstock giftstock : stockList) {
            //通过礼品编码获取礼品信息
            TabSaleplanGiftcode gift = giftService.getGift(giftstock.getGiftcode());
            LevelAndNumver levelAndNumver=new LevelAndNumver();
            //配置奖品信息配置
            levelAndNumver.setLevelName(this.getLevelName(gift.getPrizelevel()));
            levelAndNumver.setLpName(gift.getGiftname());
            levelAndNumver.setNumber(giftstock.getStocknum()+"");
            levelAndNumver.setBeData(gift.getBegintime()+"");
            levelAndNumver.setEnData(gift.getEndtime()+"");
            levelAndNumver.setYNumber(giftstock.getObtainnum()+"");
            levelAndNumver.setSale(this.getSale(gift.getSaleplanid()));
            //配置营业厅数据
            if( levelAndNumver.getGgkConfig()!=null){
                levelAndNumver.getGgkConfig().setAcptsiteid(giftstock.getAcptsiteid());
                levelAndNumver.getGgkConfig().setPrizelevel(gift.getPrizelevel()+"");
                levelAndNumver.getGgkConfig().setGiftcode(gift.getGiftcode());
            }

            lList.add(levelAndNumver);


        }
        return lList;
    }

    //根据营业厅获取地区编码
    @Override
    public String getDCode(String value) {
        String dCode;
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("code",value);
        Dict dict = this.getOne(queryWrapper);
        dCode=dict.getCode();
        while (!dict.getParent().equals("26")){
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("code",dict.getParent());
            dict= this.getOne(queryWrapper1);
            dCode+=dict.getCode();
        }
        return dCode;
    }

    @Override
    public String getLpType(String value) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent", "3");
        queryWrapper.eq("value", value);
        Dict dict = this.getOne(queryWrapper);
        return dict.getCodeName();
    }

    //根据code查询等级名称
    public String getLevelName(Integer code){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent","4");
        queryWrapper.eq("value",code);
        Dict dict = this.getOne(queryWrapper);
        return dict.getCodeName();
    }

    //根据code查询活动定义
    public String getSale(String code){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent","40");
        queryWrapper.eq("value",code);
        Dict dict = this.getOne(queryWrapper);
        return dict.getCodeName();
    }

    //通用
    public String get(String parent,String value){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent",parent);
        queryWrapper.eq("value",value);
        Dict dict = this.getOne(queryWrapper);
        return dict.getCodeName();
    }
}
